﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using RedHouse.Data.Entities;
using RedHouse.Data.Base;
using RedHouse.Data.Service;
using RedHouse.Data;
using RedHouse.Web.common;

namespace RedHouse.Web
{
    public partial class DishList :  PageBase
    {
        private IDishService _dishService;
        private ICategoryService _categoryService;
        private ILanguageService _languageService;

        public List<RedHouseDish> Dishes { get; set; }
        public string PageView { get; set; }
        public string PageTitle { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            PageView = Request.QueryString["page"] == null ? "Specialties" : Request.QueryString["page"].ToString();
            _dishService = new DishService();
            _categoryService = new CategoryService();
            _languageService = new LanguageService();

            Initialize();
        }

        private void Initialize()
        {
            SetPageTitle(PageView);
            RedHouseCategory category = _categoryService.GetCategoryByName(PageView);
            Dishes = _dishService.GetAllDishByCategory(category.Id);
            if (Session["location"] != null)
            {
                int location = int.Parse(Session["location"].ToString());
                Dishes = Dishes.Where(d => d.LocationID == location || d.LocationID == 3).ToList();
            }

            Language language;
            if (Session["Culture"] != null && Session["Culture"].ToString() == "en-US")
            {
                    language = _languageService.GetAllLanguage().Where(l => l.Description == "English").FirstOrDefault();
            }
            else
            {
                    language = _languageService.GetAllLanguage().Where(l => l.Description != "English").FirstOrDefault();
            }

            if (language != null)
            {
                Dishes = Dishes.Where(d => d.LangId == language.ID).ToList();
            }
            Dishes = Dishes.OrderBy(d => d.DisplayOrder).ToList();
            rptDish.DataSource = Dishes;
            rptDish.DataBind();
        }

        private void SetPageTitle(string pageView)
        {
            ClientScriptManager cs = Page.ClientScript;
            String csname = "PopupScript";
            Type cstype = this.GetType();
            switch (pageView)
            {
                case "Specialties":
                    PageTitle = RedHouse.Web.Resources.lblSpecialties;
                    if (!cs.IsStartupScriptRegistered(cstype, csname))
                    {
                        StringBuilder script = new StringBuilder();
                        script.Append("<script type=text/javascript>");
                        script.Append("$(document).ready(function() { $('#nav li').removeClass('active');$('#menu-specialties').addClass('active'); });");
                        script.Append("</script>");

                        cs.RegisterStartupScript(cstype, csname, script.ToString());
                    }
                    break;
                case "Seafood":
                    PageTitle = RedHouse.Web.Resources.lblSeafood;
                    if (!cs.IsStartupScriptRegistered(cstype, csname))
                    {
                        StringBuilder script = new StringBuilder();
                        script.Append("<script type=text/javascript>");
                        script.Append("$(document).ready(function() { $('#nav li').removeClass('active');$('#menu-seafood').addClass('active'); });");
                        script.Append("</script>");

                        cs.RegisterStartupScript(cstype, csname, script.ToString());
                    }
                    break;
                case "Information":
                    PageTitle = RedHouse.Web.Resources.lblInformation;
                    break;
                case "Employment":
                    PageTitle = RedHouse.Web.Resources.lblEmployment;                    
                    break;
                default:
                    break;
            }
        }
    }
}
